package com.lee.redis.service.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;

/**
 * 功能描述:
 *
 * @author liping(lpaidd50@gmail.com)
 * @version v1.0
 * @date 2017/3/14
 */
@Configuration
public class DoubleRedisConfig {

	@Value("${spring.redis.first.port}")
	int firstPort;
	@Value("${spring.redis.first.host}")
	String firstHost;
	@Value("${spring.redis.first.password}")
	String firstPassword;

	@Bean
	@Primary
	public JedisConnectionFactory firstJedisConnectionFactory() {

		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setPort(firstPort);
		jedisConnectionFactory.setHostName(firstHost);
		jedisConnectionFactory.setPassword(firstPassword);
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
//		JedisShardInfo info = new JedisShardInfo(firstHost, firstPort, firstPassword);
//		jedisConnectionFactory.setShardInfo(info);
		return jedisConnectionFactory;
	}

	@Bean(name = "firstRedisTemplate")
	@Qualifier("firstRedisTemplate")
	@Primary
	public RedisTemplate<String, String> firstRedisTemplate() {
		RedisTemplate<String, String> template = new RedisTemplate<String, String>();
		template.setConnectionFactory(firstJedisConnectionFactory());
		template.setKeySerializer(new GenericToStringSerializer(String.class));
		template.setValueSerializer(new GenericToStringSerializer(String.class));
		return template;
	}


	@Value("${spring.redis.second.port}")
	int secondPort;
	@Value("${spring.redis.second.host}")
	String secondHost;
	@Value("${spring.redis.second.password}")
	String secondPassword;


	@Bean
	public JedisConnectionFactory secondJedisConnectionFactory() {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setPort(secondPort);
		jedisConnectionFactory.setHostName(secondHost);
		jedisConnectionFactory.setPassword(secondPassword);
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
		return jedisConnectionFactory;
	}

	@Bean(name = "secondRedisTemplate")
	@Qualifier("secondRedisTemplate")
	public RedisTemplate<String, String> secondRedisTemplate() {
		RedisTemplate<String, String> template = new RedisTemplate<String, String>();
		template.setConnectionFactory(secondJedisConnectionFactory());
		template.setKeySerializer(new GenericToStringSerializer(String.class));
		template.setValueSerializer(new GenericToStringSerializer(String.class));
		return template;
	}
}
